<template>
  <div>
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <!-- <el-form-item label="所属级别" prop="role_id">
        <el-select
          clearable
          v-model="form.role_id"
          placeholder="请选择所属级别"
          style="width: 100%"
        >
          <el-option
            v-for="(item, index) in rolesList"
            :key="index"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="用户名" prop="name">
        <el-input v-model="form.name" maxlength="20" show-word-limit placeholder="请输入用户名"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          placeholder="请输入密码"
          show-password
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"/>
      </el-form-item>
      <el-form-item label="Email" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱"/>
      </el-form-item>
      <el-form-item label="登录时间" prop="">
        <span>{{ rowitem.last_login_time || '-' }}</span>
      </el-form-item>
      <el-form-item label="注册时间" prop="">
        <span>{{ rowitem.create_time || '-' }}</span>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">暂停</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div class="flexend">
      <el-button @click="handleCloseAdd">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </div>
</template>

<script>
  import { validPhone } from '@/utils/validate'
  import { editInfo } from '@/api/advmaster'

  export default {
    data() {
      // let validPassword = (rule, value, callback) => {
      //   if (value) {
      //     if (value.length < 12) {
      //       callback(new Error('密码至少12位！'))
      //     } else {
      //       callback()
      //     }
      //   } else {
      //     callback(new Error('请输入密码!'))
      //   }
      // }
      let validPassword = (rule, value, callback) => {
        if (value && value != '') {
          let pwdCount = 0
          let pwdRegex1 = new RegExp('(?=.*[0-9]).{8,30}')//纯数字
          let pwdRegex2 = new RegExp('(?=.*[a-z]).{8,30}')//小写字母
          let pwdRegex3 = new RegExp('(?=.*[A-Z]).{8,30}')//大写字母
          let pwdRegex4 = new RegExp('(?=.*[^a-zA-Z0-9]).{8,30}')//特殊字符
          if (pwdRegex1.test(value)) {
            pwdCount = pwdCount + 1
          }
          if (pwdRegex2.test(value)) {
            pwdCount = pwdCount + 1
          }
          if (pwdRegex3.test(value)) {
            pwdCount = pwdCount + 1
          }
          if (pwdRegex4.test(value)) {
            pwdCount = pwdCount + 1
          }
          if (value.length > 9) {
            pwdCount = pwdCount + 1
          }
          if (pwdCount < 5) {
            callback(new Error('密码至少包含大写字母、小写字母、数字、特殊符号中的4种，且长度至少为10位！'))
          }
          callback()
        } else {
          callback()
        }
      }
      return {
        form: {
          id: '',
          name: '',
          phone: '',
          email: '',
          status: '',
          password: ''
        },
        // 表单校验
        rules: {
          name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
          email: [
            {
              required: true,
              type: 'email',
              message: '请输入正确的邮箱地址',
              trigger: 'blur'
            }
          ],
          phone: [{ required: true, validator: validPhone, trigger: 'blur' }],
          password: [{ required: true, validator: validPassword, trigger: 'blur' }],
          status: [{ required: true, message: '请选择状态', trigger: 'change' }]
        }
      }
    },
    props: {
      rowitem: {
        default: null
      }
    },
    created() {
      this.form.id = this.rowitem.id
      this.form.name = this.rowitem.user_login
      this.form.phone = this.rowitem.mobile
      this.form.email = this.rowitem.email
      this.form.password = this.rowitem.password
      this.form.status = this.rowitem.status
    },
    methods: {
      submitForm: function() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.form.id != undefined) {
              editInfo(this.form).then(() => {
                this.$modal.msgSuccess('修改成功')
                this.$emit('close', 1)
              })
            }
          }
        })
      },

      handleCloseAdd() {
        this.$emit('close')
      }
    }
  }
</script>
